package com.wiz.netty.task;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.wiz.common.util.DateUtil;
import com.wiz.web.service.FuellingService;

/**
 * 上传订单中已支付未结算订单进行结算任务
 * @author Hakeny
 */
public class FuellingOrderCalculTask extends QuartzJobBean{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	private FuellingService fuellingService;
	

	@Override
	protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
//		Scheduler scheduler = (Scheduler) context.getScheduler(); 
		if(fuellingService == null){
			SchedulerContext schCtx;
			try {
				schCtx = context.getScheduler().getContext();
				//获取Spring中的上下文    
				ApplicationContext appCtx = (ApplicationContext)schCtx.get("applicationContext");  
				fuellingService = (FuellingService) appCtx.getBean("fuellingService");
			} catch (SchedulerException e) {
				e.printStackTrace();
			}
		}
		log.info("=========FuellingOrderCalculTask is starting===========");
		String date = DateUtil.formatTime(new Date());
		String[] strs = StringUtils.split(date,":");
		int hour = Integer.parseInt(strs[1]);
		int count = 0;
		if(hour > 0 && hour < 5){
			count = fuellingService.handlerUnCalculFuellingPayOrders(50);
		}else{
			count = fuellingService.handlerUnCalculFuellingPayOrders(20);
		}
		log.info(count+" FuellingOrderCalculTask is excuted..................");
	}
}
